package org.example.java.base;

/**
 * Created by Juntao.Zhang on 2014/8/7.
 */
public class StringTest {
  public static void main(String[] args) {
    testString(10000);
    testStringBuffer(10000000);
    testStringBuilder(10000000);
  }

  private static void testStringBuffer(int times) {
    String tempstr = "abcdefghijklmnopqrstuvwxyz";
    long lstart2 = System.currentTimeMillis();
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < times; i++) {
      sb.append(tempstr);
    }
    long lend2 = System.currentTimeMillis();
    long time2 = (lend2 - lstart2);
    System.out.println(time2);
  }

  private static void testStringBuilder(int times) {
    String tempstr = "abcdefghijklmnopqrstuvwxyz";
    long lstart2 = System.currentTimeMillis();
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < times; i++) {
      sb.append(tempstr);
    }
    long lend2 = System.currentTimeMillis();
    long time2 = (lend2 - lstart2);
    System.out.println(time2);
  }

  private static void testString(int times) {
    String tempstr = "abcdefghijklmnopqrstuvwxyz";
    long lstart1 = System.currentTimeMillis();
    String str = "";
    for (int i = 0; i < times; i++) {
      str += tempstr;
    }
    long lend1 = System.currentTimeMillis();
    long time = (lend1 - lstart1);
    System.out.println(time);
  }
}
